import { Button } from 'antd';
import { ColumnsType } from 'antd/es/table';
import { InterferenceTableData } from '.';
import { Icon, useUnit } from '@amcax/base';
import { unitManager } from '@app-base/module';

const Volume = ({ value }: { value: number }) => {
  const { lengthUnit } = useUnit();

  const unit = unitManager.getLengthUnitParams(lengthUnit);

  return `${(value / unit.multiple ** 3).toFixed(2)} ${unit.name}³`;
};

export const getInterferenceCheckColumns: (
  setTableRowIgnored: (key: string, ignored: boolean) => void,
) => ColumnsType<InterferenceTableData> = (setTableRowIgnored) => [
  {
    title: '干涉',
    key: 'label',
    width: 120,
    render(val, record, index) {
      return (
        <div className='whitespace-nowrap flex gap-x-2'>
          <Icon
            name='commands/product/interference-check'
            className='text-lg'
            style={
              record.isIgnored
                ? {
                    filter: 'grayscale(100%)',
                    opacity: 0.7,
                  }
                : null
            }
          />
          干涉{index + 1}
        </div>
      );
    },
  },
  {
    title: '零件1',
    width: 100,
    render(_, record) {
      const title = record.part1.title;
      return record.index1 == null ? title : `${title}的实体${record.index1}`;
    },
  },
  {
    title: '零件2',
    width: 100,
    render(_, record) {
      const title = record.part2.title;
      return record.index2 == null ? title : `${title}的实体${record.index2}`;
    },
  },
  {
    title: '计算结果',
    dataIndex: 'volume',
    key: 'volume',
    width: 120,
    render(val: number) {
      return <Volume value={val} />;
    },
  },
  {
    title: '',
    key: 'operation',
    dataIndex: 'isIgnored',
    width: 120,
    render(ignored: boolean, row) {
      return (
        <Button
          className='min-w-[90px]'
          onClick={() => {
            setTableRowIgnored(row.key, !ignored);
          }}
        >
          {ignored ? '取消忽略' : '忽略'}
        </Button>
      );
    },
  },
];
